﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleRobot_2
{
    public class Robot
    {
        public int _x;
        public int _y;
        public int _lastPrintedx;
        public int _lastPrintedy;
        public bool _hasBeenPrinted;
        public ConsoleColor _color;
        public GameRules _rules;

        public Robot(GameRules rules)
        {
            _x = 0;
            _y = 0;
            _color = ConsoleColor.White;
            _rules = rules;
        }
        public Robot(int x, int y, ConsoleColor color, GameRules rules)
        {
            _x = x;
            _y = y;
            _color = color;
            _rules = rules;
        }
        public void MoveLeft(int interval, GameRules rules)
        {
            _x -= interval;
            _x = Math.Max(_x, 0);
        }
        public void MoveRight(int interval, GameRules rules)
        {
            _x += interval;
            _x = Math.Min(_x, rules.fieldWidth - 1);
        }
        public void MoveTop(int interval, GameRules rules)
        {
            _y -= interval;
            _y = Math.Max(_y, 0);
        }
        public void MoveBottom(int interval, GameRules rules)
        {
            _y += interval;
            _y = Math.Min(_y, rules.fieldWidth - 1);
        }
        public void DoStep(int interval, GameRules rules, Random rnd)
        {
            int course = rnd.Next(0, 5);
            switch (course)
            {
                case 1: MoveLeft(interval, rules); break;
                case 2: MoveRight(interval, rules); break;
                case 3: MoveTop(interval, rules); break;
                case 4: MoveBottom(interval, rules); break;
            }
        }
        public void DoStep()
        {
            
            Random rnd = new Random();
            int course = rnd.Next(0, 5);
            switch (course)
            {
                case 1: MoveLeft(1, null); break;
                case 2: MoveRight(1, null); break;
                case 3: MoveTop(1, null); break;
                case 4: MoveBottom(1, null); break;
            }
        }
        public void SetPosition (int x, int  y)
        {
            Console.SetCursorPosition(x, y);
        }    
        public void Print()
        {

            if (_hasBeenPrinted)
            {
                //clear previous position
                Console.SetCursorPosition(_lastPrintedx, _lastPrintedy);
                Console.ResetColor();
                Console.Write(" ");
            }
            Console.SetCursorPosition(_x, _y);
            Console.ForegroundColor = _color;
            Console.Write("0");
            _lastPrintedx = _x;
            _lastPrintedy = _y;
            _hasBeenPrinted = true;
        }
    }
}